package com.cacho.s2b.lesson.learn;

import org.apache.commons.text.StringEscapeUtils;
import org.apache.commons.text.StringSubstitutor;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description org.apache.commons.text.StringSubstitutor 批量替换字符串
 * @Author LingWei
 * @date 2024/01/30
 **/
public class LearnCommonsText {
    public static void main (String[] args){
        stringEscapeUtils();
        Map<String, String> urlMap = new HashMap<>();
        urlMap.put("jurisdiction","?jurisdiction=GB");
        String url = "/openapi/psdcert/v2/validateTPP{jurisdiction}";
        // public <V> StringSubstitutor(Map<String, V> valueMap, String prefix, String suffix)
        StringSubstitutor sub = new StringSubstitutor(urlMap,"{","}");
        String result = sub.replace(url);
        System.out.println("替换大括号里面的jurisdiction为urlMap里面jurisdiction的value值："+result);

        // public <V> StringSubstitutor(Map<String, V> valueMap)  默认替换${}里面的值
        String url2 = "/openapi/psdcert/v2/validateTPP${jurisdiction}";
        StringSubstitutor sub2 = new StringSubstitutor(urlMap);
        System.out.println("替换${jurisdiction}为urlMap里面jurisdiction的value值："+sub2.replace(url2));

        // 可以替换多个
        String url3 = "/openapi/psdcert/v2/validateTPP${jurisdiction}${name}";
        urlMap.put("name","?name=test");
        StringSubstitutor sub3 = new StringSubstitutor(urlMap);
        System.out.println("替换${jurisdiction}${name}为urlMap里面jurisdiction和name的value值："+sub3.replace(url3));

        // 可以替换多个
        String url4 = "${account}/openapi/accounts/v3/01700020402SAR/balances";
        urlMap.put("account","https://ms-accounts-openapi-api-banking-dev.apps.dcda-np2.ocp.dev.net");
        StringSubstitutor sub4 = new StringSubstitutor(urlMap);
        System.out.println("替换${account}："+sub4.replace(url4));
    }

    public static void stringEscapeUtils (){
        String value = "{\"Data\":{\"Permissions\":[\"AccountBasic\",\"AccountDetails\",\"AccountsBalance\"]}}" ;

        // 转义Java字符串中的特殊字符，使其成为Java代码中可识别的形式,
        String text = StringEscapeUtils.escapeJava(value);
        // 对JSON字符串进行转义处理，以避免在JSON解析时出现特殊字符导致的解析错误或安全问题
        String jsonText = StringEscapeUtils.escapeJson(value);
        // 用于解码Java字符串常量中的转义序列，如\n、\t和反斜杠等
        String pText = StringEscapeUtils.unescapeJava(value);

    }
}
